package com.huluobo.user.pages

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseLoadingDialog
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.StateType
import com.huluobo.lib_base.UIState
import com.huluobo.user.R
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.huluobo.user.entity.ApproveEntity
import com.huluobo.user.entity.ApproveEntityItem
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/7/31.
 */

@Composable
fun ComListPage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    val list = remember {
        SnapshotStateList<ApproveEntityItem>()
    }

    var isShowProgress by remember {
        mutableStateOf(false)
    }

    var index by remember {
        mutableStateOf(-1)
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect {
                when (it) {
                    is UIState.OnFailed -> {
                        isShowProgress = false
                        Log.e("TagA", "获取审批列表失败:${it.msg}")
                        ToastUtils.showLong("获取审批列表失败:${it.msg}")
                    }

                    UIState.OnLoading -> {
                        isShowProgress = true
                    }

                    is UIState.OnSuccess<*> -> {
                        isShowProgress = false
                        if (it.type == StateType.DEFAULT) {
                            list.clear()
                            list.addAll((it.data as ApproveEntity).filter { item ->
                                item.status == "未审核"
                            })
                        }

                        if (it.type == StateType.DELETE) {
                            if (index != -1) {
                                ToastUtils.showLong("删除成功")
                                list.removeAt(index)
                            }
                        }
                    }
                }
            }
        }
        vm.sendIntent(UserIntent.GetApprove)
    })

    if (isShowProgress) {
        BaseLoadingDialog()
    }

    Column(Modifier.padding(10.dp)) {
        BaseTitle(title = "审批列表", navHostController = navHostController)
        Spacer(modifier = Modifier.height(10.dp))
        Row(
            Modifier
                .fillMaxWidth()
                .background(color = colorResource(id = R.color.lightblue))
                .padding(10.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            var sort = true
            Text(text = "类型", modifier = Modifier.clickable {
                if (sort) {
                    sort = false
                    list.sortBy { it.type }
                } else {
                    sort = true
                    list.sortByDescending { it.type }
                }
            })
            Text(text = "开始时间", modifier = Modifier.clickable {
                if (sort) {
                    sort = false
                    list.sortBy { it.startTime }
                } else {
                    sort = true
                    list.sortByDescending { it.startTime }
                }
            })
            Text(text = "结束时间", modifier = Modifier.clickable {
                if (sort) {
                    sort = false
                    list.sortBy { it.endTime }
                } else {
                    sort = true
                    list.sortByDescending { it.endTime }
                }
            })
            Text(text = "审批状态")
        }

        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(content = {
            itemsIndexed(list) { i, item ->
                ApproveItem(entityItem = item, navHostController, vm) {
                    index = i
                }
            }
        }, verticalArrangement = Arrangement.spacedBy(10.dp))
    }
}